package com.moqianfan.mcuassistant.fragment;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.moqianfan.mcuassistant.bean.User;
import com.moqianfan.mcuassistant.comm.AppConfig;
import com.moqianfan.mcuassistant.comm.Common;

import java.util.HashSet;
import java.util.Set;

import butterknife.ButterKnife;
import cn.bmob.v3.BmobUser;

/**
 * Created by 李茂红 on 2016/4/3 0003.
 * fragment基类
 */
public abstract class BaseFragment extends Fragment {
    public SharedPreferences userPreference;
    public User user;
    public AppConfig appConfig;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        appConfig = AppConfig.getAppConfig();
        userPreference = getActivity().getSharedPreferences(Common.SP_USER, Context.MODE_APPEND);
        user = BmobUser.getCurrentUser(User.class);
    }

    public static Set<BaseFragment> fragmentSet = new HashSet<>();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = findViews(inflater, container);
        ButterKnife.bind(this, view);
        initViews();
        loadData();
        return view;
    }


    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        fragmentSet.add(this);
    }

    @Override
    public void onDetach() {
        fragmentSet.remove(this);
        super.onDetach();

    }

    /**
     * 设置fragment的布局资源,找到需要的控件
     */
    public abstract View findViews(LayoutInflater inflater, ViewGroup container);

    /**
     * 设置控件的一些属性
     */
    public abstract void initViews();

    /**
     * 加载界面数据
     */
    public abstract void loadData();

    public void showToastShort(String msg) {
        Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT).show();
    }

    public void showToastLong(String msg) {
        Toast.makeText(getActivity(), msg, Toast.LENGTH_LONG).show();
    }

    public void openActivity(Class<?> calss) {
        Intent intent = new Intent();
        intent.setClass(getActivity(), calss);
        startActivity(intent);
    }

    public void openService(Class<?> calss) {
        Intent intent = new Intent();
        intent.setClass(getActivity(), calss);
        getActivity().startService(intent);
    }

    public void stopService(Class<?> calss) {
        Intent intent = new Intent();
        intent.setClass(getActivity(), calss);
        getActivity().stopService(intent);
    }

    public void sendMsg(String msg, int what, Handler handler) {
        Bundle bundle = new Bundle();
        bundle.putString("msg", msg);
        Message message = new Message();
        message.what = what;
        message.setData(bundle);
        handler.sendMessage(message);
    }
}
